package com.ljd.service.impl;

import com.ljd.entity.Book;
import com.ljd.entity.Borrow;
import com.ljd.repository.BookRepository;
import com.ljd.repository.BorrowRepository;
import com.ljd.repository.impl.BookRepositoryImpl;
import com.ljd.repository.impl.BorrowRepositoryImpl;
import com.ljd.service.BookService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BookServiceImpl implements BookService {
    private BookRepository bookRepository = new BookRepositoryImpl();
    private BorrowRepository borrowRepository = new BorrowRepositoryImpl();
    private final Integer LIMIT = 5;

    @Override
    public List<Book> findAllBooks(int page) {
        Integer index = LIMIT * (page - 1);
        return bookRepository.findAllBooks(index, LIMIT);
    }

    @Override
    public Integer getPagesCount() {
        Integer count = bookRepository.getCount();
        Integer page = null;
        if (count % LIMIT == 0) {
            page = count / LIMIT;
        } else {
            page = (count / LIMIT) + 1;
        }
        return page;
    }

    @Override
    public void addBorrow(Integer bookid, Integer readerid) {
        //借书时间
        Date date = new Date();//当前系统时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = dateFormat.format(date);

        //还书时间，借书时间+14天 Calendar工具类
        Calendar calendar = Calendar.getInstance();
        //获取当前是第几天
        Integer dateInt = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.set(Calendar.DAY_OF_YEAR, dateInt + 14);
        Date date2 = calendar.getTime();//归还日期
        String returnTime = dateFormat.format(date2);

        borrowRepository.insert(bookid, readerid, borrowTime,returnTime,null);
    }

    @Override
    public List<Borrow> findAllBorrowByReaderId(Integer page,Integer readerid) {
        Integer index = LIMIT * (page - 1);
        return borrowRepository.findAllByReaderId(readerid, index, LIMIT);
    }

    @Override
    public Integer getBorrowPagesCount(Integer readerid) {
        Integer count = borrowRepository.getCountByReaderid(readerid);
        Integer page = null;
        if (count % LIMIT == 0) {
            page = count / LIMIT;
        } else {
            page = (count / LIMIT) + 1;
        }
        return page;
    }

    @Override
    public Integer getBorrowPagesCountByState(Integer state) {
        Integer count = borrowRepository.getCountByState(state);
        Integer page = null;
        if (count % LIMIT == 0) {
            page = count / LIMIT;
        } else {
            page = (count / LIMIT) + 1;
        }
        return page;
    }

    @Override
    public void handleBorrow(Integer borrowid, Integer state, Integer adminid) {
        borrowRepository.handleBorrow(borrowid, state, adminid);
    }

    @Override
    public List<Borrow> findAllBorrowByState(Integer page, Integer state) {
        Integer index = LIMIT * (page - 1);
        return borrowRepository.findAllByState(state, index, LIMIT);
    }
}
